/*
 * @Author: @yzcheng
 * @Date: 2021-02-26 18:44:54
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description:
 * @LastEditTime: 2021-05-20 10:58:25
 */
import {
  flowDownload,
  deploymentProcess,
  deleteByFlowId,
  suspendAllById,
  flowView,
  getFlowDefineList,
} from '@/api/flowDefineApi.js'
export default {
  namespaced: true,
  state: () => ({
    FlowPageData: [], // 流程定义分页数据
    dialogChart: false,
    form: {
      // 流程定义form表单值
      flowName: '',
      flowKey: '',
      flowType: '',
      flowStatus: '',
      flowTypeName: '',
    },
    pageNum: 1,
    total: 0,
    pageSize: 10,
  }),
  getter: {},
  actions: {
    /**
     *下载文件
     *
     * @param {*} { state }
     * @param {*} params
     * @return {*}
     */
    // eslint-disable-next-line no-unused-vars
    async getFlowDownload({ state, commit }, params) {
      return await flowDownload(params)
        .then((res) => {
          return res
        })
        .catch(() => {
          return 2
        })
    },
    /**
     *预览
     *
     * @param {*} { state }
     * @param {*} params
     * @return {*}
     */
    // eslint-disable-next-line no-unused-vars
    async getFlowChart({ state, commit }, params) {
      return await flowView(params)
    },
    /**
     *部署流程
     *
     * @param {*} { state }
     * @param {*} { id }
     */
    // eslint-disable-next-line no-unused-vars
    async performDeploymentProcess({ state }, { id }) {
      await deploymentProcess(id)
    },
    /**
     *根据流程定义文件id删除部署的流程
     *
     * @param {*} { state }
     * @param {*} id
     */
    // eslint-disable-next-line no-unused-vars
    async performDeleteByFlowId({ state, dispatch }, id) {
      await deleteByFlowId(id)
      await dispatch('getFlowDefineList')
    },
    /**
     *根据文件id挂起全部部署的流程
     *
     * @param {*} { state }
     * @param {*} id
     */
    // eslint-disable-next-line no-unused-vars
    async performSuspendAllById({ dispatch }, { procDefId, suspend }) {
      await suspendAllById({ procDefId, suspend })
      await dispatch('getFlowDefineList')
    },
    /**
     *  流程分页查询方法
     *
     * @param {*} { state }
     * @param {*} payload
     */
    async getFlowDefineList({ state }, payload) {
      let { pageNum, pageSize } = state
      return await getFlowDefineList(state.form, { pageNum, pageSize })
        .then((res) => {
          console.log(res,'getFlowDefineList');
          if (res.data.success) {
            state.FlowPageData = res?.data.data
            state.pageNum = res?.data?.data.current
            state.total = res?.data?.data.total
            state.pageSize = res?.data?.data.size
          }
          return res.data.code
        })
        .catch(() => {
          return undefined
        })
    },
    /**
     *  流程定义管理重置
     *
     * @param {*} {state,commit,dispatch}
     * @param {*} payload
     */
    // eslint-disable-next-line no-unused-vars
    reset({ state, commit, dispatch }, payload) {
      state.form = {
        // 流程定义form表单值
        flowName: '',
        flowKey: '',
        flowType: '',
        flowStatus: '',
      }
    },
    /**
     *  查看view流程图
     *
     * @param {*} {state,commit,dispatch}
     * @param {*} payload
     */
    // eslint-disable-next-line no-unused-vars
    async getCurrentCanvasXml(
      // eslint-disable-next-line no-unused-vars
      { state, commit, dispatch },
      { procDefId, callback }
    ) {
      const rst = await dispatch('getFlowChart', procDefId)
      callback(rst)
    },
  },
  mutations: {
    savePage(state, payload) {
      state.pageNum = payload
    },
  },
}
